home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex8-16.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  2KB  |  74 lines

  1. // ex-16.c -- Property list
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex8-16.c,v 3.0 90/05/15 22:46:13 kgorlen Rel $
  4.  
  5. #include "IdentDict.h"
  6. #include "Dictionary.h"
  7. #include "String.h"
  8. #include "Date.h"
  9. #include "Patient.h"
  10.  
  11. class Property: public NIHCL {
  12.     static IdentDict prop;      // object property lists
  13. public:
  14.     static Object* add(Object& owner, String& name, Object& value);
  15.     static Object* get(const Object& owner, const String& name);
  16. };
  17.  
  18. IdentDict Property::prop;
  19.  
  20. Object* Property::add(
  21.     Object& owner,      // object to receive property
  22.     String& name,       // name of property
  23.     Object& value)      // property value
  24. {
  25.     Object* oldvalue = &value;
  26.     Dictionary* d;
  27.     if (!prop.includesKey(owner)) {
  28.         d = new Dictionary;
  29.         d->addAssoc(*new String(name),value);
  30.         prop.addAssoc(owner,*d);
  31.     }
  32.     else {
  33.         d = (Dictionary*)prop.atKey(owner);
  34.         if (d->includesKey(name))
  35.             oldvalue = d->atKey(name,value);
  36.         else d->addAssoc(*new String(name),value);
  37.     }
  38.     return oldvalue;
  39. }
  40.  
  41. Object* Property::get(
  42.     const Object& owner,// object with property
  43.     const String& name) // name of property
  44. {
  45.     if (!prop.includesKey(owner)) return Object::nil;
  46.     Dictionary* d = (Dictionary*)prop.atKey(owner);
  47.     if (d->includesKey(name)) return d->atKey(name);
  48.     else return Object::nil;
  49. }
  50.  
  51. main()
  52. {
  53. // First patient
  54.     Patient p1("Fried Harry I.","987-65-4321",22221);
  55.     Property::add(p1,
  56.         *new String("admission date"),*new Date(10,"Mar",86));
  57.     Property::add(p1,
  58.         *new String("complaint"),*new String("fever"));
  59.  
  60. // Second patient
  61.     Patient p2("Chavez Maria G.","444-555-6666",22223);
  62.     Property::add(p2,
  63.         *new String("admission date"),*new Date(20,"Mar",86));
  64.     Property::add(p2,
  65.         *new String("complaint"),*new String("broken leg"));
  66.  
  67. // Print some properties of Patient objects
  68.     cout << *Property::get(p1,"admission date") << endl;
  69.     cout << *Property::get(p1,"complaint") << endl;
  70.     cout << *Property::get(p2,"admission date") << endl;
  71. // Non-existent property
  72.     cout << *Property::get(p2,"birthdate") << endl;
  73. }
  74.